package com.baichang.taoshunda.controller.mobile;


import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.dao.BussinessDao;
import com.baichang.taoshunda.dao.GoodsDao;
import com.baichang.taoshunda.dao.ScoreGoodsDao;
import com.baichang.taoshunda.dao.TrendsDao;
import com.baichang.taoshunda.entity.Bussiness;
import com.baichang.taoshunda.entity.Goods;
import com.baichang.taoshunda.entity.Html;
import com.baichang.taoshunda.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by Dylan on 2017/6/5.
 */
@Controller
@RequestMapping(ControllerConstants.WEBVIEW)
public class WebVideMController extends WebBaseController {


    @Autowired
    private PushService pushService;
    @Autowired
    private ParamsService paramsService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private WaiterService waiterService;
    @Autowired
    private ScoreGoodsDao scoreGoodsDao;
    @Autowired
    private HtmlService htmlService;
    @Autowired
    private IdleGoodsService idleGoodsService;

    @Autowired
    private CommonProblemService commonProblemService;
    @Autowired
    private BussGoodsTypeService bussGoodsTypeService;
    @Autowired
    GoodsDao goodsDao;
    @Autowired
    BussinessDao bussinessDao;
    @Autowired
    TrendsDao trendsDao;

    /**
     * 关于我们
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/aboutUs")
    public String aboutUs(HttpServletRequest request, HttpServletResponse response, Model model) {
        Map content = paramsService.getParamsValue(20, 12);
        if (content == null || content.size() < 1) {
            return "taoshunda/error";
        } else {
            model.addAttribute("content", content.get("remark"));
            return "taoshunda/aboutUs";
        }

    }

    /**
     * 隐私协议
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/privacy")
    public String privacy(HttpServletRequest request, HttpServletResponse response, Model model) throws ServiceException {
        Html content = htmlService.queryById(9);
        if (content == null) {
            return "taoshunda/error";
        } else {

        }
        model.addAttribute("content", content.getValue());
        return "taoshunda/aboutUs";
    }

    /**
     * 用户协议
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/userAgreement")
    public String userAgreement(HttpServletRequest request, HttpServletResponse response, Model model) throws ServiceException {
        Html content = htmlService.queryById(1);
        if (content == null) {
            return "taoshunda/error";
        } else {
            System.out.println(content.getValue());
            model.addAttribute("content", content.getValue());
            return "taoshunda/aboutUs";
        }

    }

    /**
     * 消息详情
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/pushDetail")
    public String pushDetail(HttpServletRequest request, HttpServletResponse response, Model model) {
        Integer pushId = ServletRequestUtils.getIntParameter(request, "pushId", 0);
        Map<String, Object> map = pushService.getPushDetail(pushId);
        if (map == null || map.size() < 1) {
            return "taoshunda/error";
        } else {
            model.addAttribute("map", map);
            return "taoshunda/pushDetail";
        }

    }

    /**
     * 商家分享
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
//    @RequestMapping("/bussDetail")
//    public String bussDetail(HttpServletRequest request, HttpServletResponse response, Model model) throws ServiceException {
//        Integer busId = ServletRequestUtils.getIntParameter(request, "busId", 0);
//        Bussiness company = bussinessDao.getCompanyById(busId);
//        if (company == null) {
//            return "taoshunda/error";
//        } else {
//            Map<String, Object> map = businessService.getBussinessDateial(busId, 36.66675883572257, 117.1441678057673, 16);
//            model.addAttribute("map", map);
//            return "taoshunda/bussDetail";
//        }
//    }
    @RequestMapping("/bussDetail")
    public String bussDetail(HttpServletRequest request, HttpServletResponse response, Model model) throws ServiceException {
        Integer busId = ServletRequestUtils.getIntParameter(request, "busId", 0);
        Bussiness company = bussinessDao.getCompanyById(busId);
        if (company == null) {
            return "taoshunda/error";
        } else {
            Map<String, Object> map = businessService.getBussinessDateial(busId, 36.66675883572257, 117.1441678057673, 16);
            String bannerImages = (String) map.get("bannerImages");
            String[] bannerImagesArr = bannerImages.split(",");
            model.addAttribute("map", map);
            model.addAttribute("busId", busId);
            model.addAttribute("bannerImagesArr", bannerImagesArr);
            int userId = 0;
            List<Map<String, Object>> goods = bussGoodsTypeService.getGoodsTypeByBussId(busId, userId);
            model.addAttribute("goods", goods);
            return "taoshunda/newBusDetail";
        }
    }

    @RequestMapping("/Detail")
    public String Detail(HttpServletRequest request, HttpServletResponse response, Model model) throws ServiceException {
        Integer busId = ServletRequestUtils.getIntParameter(request, "busId", 0);
        Bussiness company = bussinessDao.getCompanyById(busId);
        if (company == null) {
            return "taoshunda/error";
        } else {
            Map<String, Object> map = businessService.getBussinessDateial(busId, 36.66675883572257, 117.1441678057673, 16);
            System.out.println(map);
            System.out.println("查询map");
            model.addAttribute("map", map);
            return "taoshunda/bussDetail";
        }
    }


    /**
     * 本地服务分享
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/waiterService")
    public String waiterService(HttpServletRequest request, HttpServletResponse response, Model model) {
        Integer id = ServletRequestUtils.getIntParameter(request, "id", 0);
        Map<String, Object> map = waiterService.getWaiterByid(id);
        if (map == null || map.size() < 1) {
            return "taoshunda/error";
        } else {
            model.addAttribute("map", map);
            return "taoshunda/waiterService";
        }

    }

    /**
     * 商品分享
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/goodsDetail")
    public String goodsDetailShare(HttpServletRequest request, HttpServletResponse response, Model model) throws ServiceException {
        Integer id = ServletRequestUtils.getIntParameter(request, "id", 0);
        Goods goods = goodsDao.getGoodsById(id);
        if (goods == null) {
            return "taoshunda/error";
        } else {
            Map<String, Object> map = goodsService.getGoodsDetails(id, 0.0, 0.0, 0);
            model.addAttribute("map", map);
            return "taoshunda/goodsDetail";
        }

    }

    /**
     * 积分商品分享
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/scoreGoodsDetail")
    public String scoreGoodsDetail(HttpServletRequest request, HttpServletResponse response, Model model) {
        Integer id = ServletRequestUtils.getIntParameter(request, "id", 0);
        Map<String, Object> map = scoreGoodsDao.getScoreGoodsById(id);
        if (map == null || map.size() < 1) {
            return "taoshunda/error";
        } else {
            model.addAttribute("map", map);
            return "taoshunda/scoreGoodsDetail";
        }

    }


    /**
     * 闲置商品分享
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/idleGoods")
    public String goodsDetail(HttpServletRequest request, HttpServletResponse response, Model model) {
        Integer id = getInt(request, "id", 0);
        Map<String, Object> map = idleGoodsService.getIdleGoodsById(id);
        System.out.println(map);
        if (map == null || map.size() < 1) {
            return "taoshunda/error";
        } else {
            model.addAttribute("map", map);
            return "taoshunda/idleGoods";
        }

    }

    /**
     * 查询常见问题
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryAnswer")
    public String queryAnswer(HttpServletRequest request, HttpServletResponse response, Model model) {
        List<Map<String, Object>> list = commonProblemService.queryAnswer(1);
        System.out.println(list);
        model.addAttribute("a", list);
        return "taoshunda/problem";
    }

    /**
     * 互动分享
     *
     * @param request
     * @param response
     */
    @RequestMapping("/shareTrends")
    public String shareTrends(HttpServletRequest request, HttpServletResponse response, Model model) {
        Integer userId = getInt(request, "userId", 0);
        Integer trendId = getInt(request, "trendId", 0);
//        Trends trends = trendsDao.getEntityById(trendId);
//        if (trends == null) {
//            return "taoshunda/error";
//        } else {
//            Map<String, Object> map = appTrendsService.getTrendsDetail(trendId, userId);
//            model.addAttribute("map", map);
        return "taoshunda/trendShare";
//        }

    }

    /**
     * 在线购买服务用户协议
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/orderAgreement")
    public String orderAgreement(HttpServletRequest request, HttpServletResponse response, Model model) throws ServiceException {
        Html content = htmlService.queryById(10);
        if (content == null) {
            return "taoshunda/error";
        } else {
            model.addAttribute("content", content.getValue());
            return "taoshunda/aboutUs";
        }

    }

    /**
     * 淘瞬达APP水电煤—用户缴费协议
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/serviceAgreement")
    public String serviceAgreement(HttpServletRequest request, HttpServletResponse response, Model model) throws ServiceException {
        Html content = htmlService.queryById(11);
        if (content == null) {
            return "taoshunda/error";
        } else {
            model.addAttribute("content", content.getValue());
            return "taoshunda/aboutUs";
        }

    }
}
